﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.Extend.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Repositories;
using Estsh.Core.Wms.IRepositories;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Repositories
{
    public class ReworkAndRepairRepository : BaseRepository<BaseEntity>, IReworkAndRepairRepository
    {


        public ReworkAndRepairRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }



        /// <summary>
        /// 根据登录账号获取工作中心
        /// </summary>
        /// <param name="empId"></param>
        /// <returns></returns>
        public List<Syspdline_Extend> GetSysPdlineList(string empId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
             
                SqlStringBuilder.Append(" SELECT * FROM dbo.sys_pdline pd (nolock) WHERE pd.pdline_name IN  (SELECT a.name  from dbo.sys_web_menu as A (nolock) ");
                SqlStringBuilder.Append(" INNER join dbo.sys_role_privliege as B (nolock)  on cast(a.menu_id  as nvarchar)=b.fun_name ");
                SqlStringBuilder.Append(" INNER join sys_role_emp as D (nolock)  on d.role_id=b.role_id ");
                SqlStringBuilder.Append(" INNER join sys_emp as E (nolock)  on e.emp_id=d.emp_id ");
                SqlStringBuilder.Append(" where  e.emp_id='" + empId + "' AND a.parent_id=60 AND a.sort_num>0 AND a.name!='PDA' AND a.enabled='Y'  ) AND pd.enabled='Y' ORDER BY pd.pdline_name ");

                List<Syspdline_Extend> result = dbConn.Query<Syspdline_Extend>(SqlStringBuilder.ToString()).ToList();

                return result;
            }
        }

        /// <summary>
        /// 根据工作中心来查询工单
        /// </summary>
        /// <param name="pdlineId"></param>
        /// <returns></returns>
        public List<MesWorkOrder_Extend> GetWorkOrderByPdlineId(string pdlineId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append(" SELECT w.* FROM dbo.mes_work_order w  (NOLOCK)  LEFT JOIN dbo.sys_pdline p  (NOLOCK) ON w.pdline_id=p.pdline_id  ");
                SqlStringBuilder.Append(" WHERE w.enabled='Y'AND p.enabled='Y'  and w.order_status = 30  and order_type="+ (int)EnumUtil_Extend.MesWorkOrderType.REWORK + " ");//工单状态为启动
                if (pdlineId.Length > 0)
                {
                    SqlStringBuilder.Append(" AND w.pdline_id='" + pdlineId + "' ORDER BY w.produce_time ASC ");
                }

                List<MesWorkOrder_Extend> result = dbConn.Query<MesWorkOrder_Extend>(SqlStringBuilder.ToString()).ToList();

                return result;
            }
        }


        /// <summary>
        /// 扫描箱条码，验证条码是否在工单中
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public List<MesWorkOrder_Extend> GetWorkOrderByCartonNo(string orderNo , string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append(" select w.* from mes_work_order (nolock)  w inner join sys_stock (nolock) s on w.part_id=s.part_id  ");
                SqlStringBuilder.Append(" where w.enabled='Y' AND s.Enabled='Y' and w.order_status=30 and w.order_type="+ (int)EnumUtil_Extend.MesWorkOrderType.REWORK + " AND w.order_no='" + orderNo + "' and  s.carton_no='"+ cartonNo + "'   ");// 工单为启动
                List<MesWorkOrder_Extend> result = dbConn.Query<MesWorkOrder_Extend>(SqlStringBuilder.ToString()).ToList();

                return result;
            }
        }

        /// <summary>
        /// 扫描箱条码，查找stock表数据
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public List<SysStock_Extend> GetStockByCartonNo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append(" SELECT * FROM sys_stock where Enabled='Y' AND carton_no='"+ cartonNo + "'   ");
                List<SysStock_Extend> result = dbConn.Query<SysStock_Extend>(SqlStringBuilder.ToString()).ToList();

                return result;
            }
        }

        /// <summary>
        /// 查找产线信息
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public List<Syspdline_Extend> GetPdlineByPdlineId(string PdlineId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append(" SELECT * from sys_pdline where Enabled='Y' AND pdline_id='"+ PdlineId + "'   ");
                List<Syspdline_Extend> result = dbConn.Query<Syspdline_Extend>(SqlStringBuilder.ToString()).ToList();

                return result;
            }
        }


        /// <summary>
        ///显示bom /通过工单零件显示bom信息
        /// </summary>
        /// <param name="wmsRack"></param>
        public void GetBomDetailListDown(decimal? qty,string partId, ref List<SysBomDetail_Extend> GetAllBomDetail)
        {

            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder stringBuilderBomDetail = new StringBuilder();
                if (qty > 0)
                {
                    stringBuilderBomDetail.Append(@"select (isnull(item_qty,'0')*"+ qty + ")AS itemQtyAndQty, *  from sys_bom_detail (nolock) where Enabled='Y' and part_id='" + partId + "' ");//根据零件号的父级id，查询bom明细表
                }
                else
                {
                    stringBuilderBomDetail.Append(@"select * from sys_bom_detail (nolock) where Enabled='Y' and part_id='" + partId + "' ");//根据零件号的父级id，查询bom明细表
                }
               

                List<SysBomDetail_Extend> sysBomDetails = dbConn.Query<SysBomDetail_Extend>(stringBuilderBomDetail.ToString()).ToList();
                if (sysBomDetails.Count != 0)
                {
                    foreach (var item in sysBomDetails)
                    {
                        GetAllBomDetail.Add(item);
                        GetBomDetailListDown(qty, item.ItemPartId.ToString(), ref GetAllBomDetail);
                    }

                }

            }

        }








    }
}
